package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyCollectionTrainAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.OrderListAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Order;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/12.
 */

public class OrderActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView allorder, endorder, waitorder;
    private RecyclerView recyclerView;
    private SharedPreferences sp;
    private int uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        initView();
        initEvent();

    }

    private void initView() {
        recyclerView = (RecyclerView)findViewById(R.id.rv_order);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        back = (ImageView) findViewById(R.id.my_order_back);
        allorder = (TextView) findViewById(R.id.allorder);
        endorder = (TextView) findViewById(R.id.endorder);
        waitorder = (TextView) findViewById(R.id.waitorder);
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        uid = sp.getInt("uid",4);
        MyModel myModel = new MyModel();
        myModel.allorder(uid,allorderlistener);
    }

    List_TListener<Order> allorderlistener = new List_TListener<Order>() {
        @Override
        public void onResponse(final List<Order> list) {
            OrderListAdapter orderListAdapter = new OrderListAdapter(getApplicationContext(),list);
            recyclerView.setAdapter(orderListAdapter);
            orderListAdapter.setmOnItemClickListener(new OrderListAdapter.mOnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                   final int oid = list.get(position).getId();
                    switch (view.getId()){
                        case R.id.order_del:

                            final MyModel myModel = new MyModel();
                            myModel.delorder(oid, new TListener<Success>() {
                                @Override
                                public void onResponse(Success success) {
                                    Toast.makeText(OrderActivity.this,"删除订单成功", Toast.LENGTH_SHORT).show();
                                    myModel.allorder(uid,allorderlistener);
                                }

                                @Override
                                public void onFail(String msg) {

                                }
                            });
                            break;
                        case R.id.order_return_pay:
                            //申请退款or付款
                           final int orderFlag = list.get(position).getFlag();
                            if(orderFlag==1){
                                //申请退款
                                AlertDialog.Builder builder = new AlertDialog.Builder(OrderActivity.this);
                                builder.setTitle("付款");
                                builder.setMessage("是否要申请退款?");
                                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        new MyModel().delorder(oid, new TListener<Success>() {
                                            @Override
                                            public void onResponse(Success success) {
                                                Toast.makeText(OrderActivity.this,"退款成功", Toast.LENGTH_SHORT).show();
                                            }

                                            @Override
                                            public void onFail(String msg) {

                                            }
                                        });
                                        new MyModel().allorder(uid,allorderlistener);
                                    }
                                });
                                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                });
                                AlertDialog dialog = builder.create();
                                dialog.show();

                            }else if(orderFlag==2){
                                //付款
                                AlertDialog.Builder builder = new AlertDialog.Builder(OrderActivity.this);
                                builder.setTitle("付款");
                                builder.setMessage("是否要支付10000元进行支付?");
                                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Toast.makeText(OrderActivity.this,"支付成功", Toast.LENGTH_SHORT).show();
                                        //将订单更改为已完成订单
                                        //.......
                                        final MyModel myModel1 = new MyModel();
                                        myModel1.changeOrder(oid, new TListener<Success>() {
                                            @Override
                                            public void onResponse(Success success) {
                                                myModel1.allorder(uid,allorderlistener);
                                            }

                                            @Override
                                            public void onFail(String msg) {

                                            }
                                        });
                                    }
                                });
                                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                });
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                            break;
                    }
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };
    List_TListener<Order> endorderlis = new List_TListener<Order>() {
        @Override
        public void onResponse(final List<Order> list) {
            OrderListAdapter orderListAdapter = new OrderListAdapter(getApplicationContext(), list);
            recyclerView.setAdapter(orderListAdapter);
            orderListAdapter.setmOnItemClickListener(new OrderListAdapter.mOnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    final int oid = list.get(position).getId();
                    switch (view.getId()){
                        case R.id.order_del:

                            final MyModel myModel = new MyModel();
                            myModel.delorder(oid, new TListener<Success>() {
                                @Override
                                public void onResponse(Success success) {
                                    Toast.makeText(OrderActivity.this,"删除订单成功", Toast.LENGTH_SHORT).show();
                                    myModel.endorder(uid,endorderlis);
                                }

                                @Override
                                public void onFail(String msg) {

                                }
                            });
                            break;
                        case R.id.order_return_pay:
                                //申请退款
                            AlertDialog.Builder builder = new AlertDialog.Builder(OrderActivity.this);
                            builder.setTitle("付款");
                            builder.setMessage("是否要申请退款?");
                            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    new MyModel().delorder(oid, new TListener<Success>() {
                                        @Override
                                        public void onResponse(Success success) {
                                            new MyModel().delorder(oid, new TListener<Success>() {
                                                @Override
                                                public void onResponse(Success success) {
                                                    Toast.makeText(OrderActivity.this,"退款成功", Toast.LENGTH_SHORT).show();
                                                }

                                                @Override
                                                public void onFail(String msg) {

                                                }
                                            });
                                            new MyModel().endorder(uid,endorderlis);
                                        }

                                        @Override
                                        public void onFail(String msg) {

                                        }
                                    });
                                    new MyModel().allorder(uid,allorderlistener);
                                }
                            });
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();

                            break;
                    }
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };
    List_TListener<Order> waitorderlis = new List_TListener<Order>() {
        @Override
        public void onResponse(final List<Order> list) {
            OrderListAdapter orderListAdapter = new OrderListAdapter(getApplicationContext(),list);
            recyclerView.setAdapter(orderListAdapter);
            orderListAdapter.setmOnItemClickListener(new OrderListAdapter.mOnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    final int oid = list.get(position).getId();
                    switch (view.getId()){
                        case R.id.order_del:
                            final MyModel myModel = new MyModel();
                            myModel.delorder(oid, new TListener<Success>() {
                                @Override
                                public void onResponse(Success success) {
                                    Toast.makeText(OrderActivity.this,"删除订单成功", Toast.LENGTH_SHORT).show();
                                    myModel.waitorder(uid,waitorderlis);
                                }

                                @Override
                                public void onFail(String msg) {

                                }
                            });
                            break;
                        case R.id.order_return_pay:
                            //付款
                            AlertDialog.Builder builder = new AlertDialog.Builder(OrderActivity.this);
                            builder.setTitle("付款");
                            builder.setMessage("是否要支付10000元进行支付?");
                            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(OrderActivity.this,"支付成功", Toast.LENGTH_SHORT).show();
                                    //将订单更改为已完成订单
                                    //.......
                                    final MyModel myModel1 = new MyModel();
                                    myModel1.changeOrder(oid, new TListener<Success>() {
                                        @Override
                                        public void onResponse(Success success) {
                                            myModel1.waitorder(uid,waitorderlis);
                                        }

                                        @Override
                                        public void onFail(String msg) {

                                        }
                                    });
                                }
                            });
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();
                            break;
                    }
                }
            });

        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        allorder.setOnClickListener(this);
        endorder.setOnClickListener(this);
        waitorder.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        MyModel myModel = new MyModel();
        switch (v.getId()) {
            case R.id.my_order_back:
                finish();
                break;
            case R.id.endorder:
                myModel.endorder(uid,endorderlis);

                break;
            case R.id.allorder:
                myModel.allorder(uid,allorderlistener);
                break;
            case R.id.waitorder:
                myModel.waitorder(uid,waitorderlis);
                break;


        }
    }
}
